const express = require("express");
const path = require("path");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
// 使用环境变量
require("dotenv").config();

const errors = require("./middleware/errors");
const router = require("./routes");

const app = express();

app
  .use(cors()) // 处理跨域
  .use(express.json()) // parsing application/json
  .use(express.urlencoded({ extended: true })) // parsing application/x-www-form-urlencoded
  .use(logger("short")) // 日志打印
  .use(cookieParser()) // parse cookie
  .use(express.static(path.join(__dirname, "public"))) // 静态文件夹暴露
  .use(router)
  // 错误处理中间件，必须放到最后
  .use(errors);

module.exports = app;
